<%@include file="_header.jspf" %>
<%@page import="com.spartancoder.accommodation.models.University" %>
<%@page import="com.spartancoder.accommodation.models.College" %>
<%@page import="com.spartancoder.accommodation.models.Place" %>
<%@page import="com.spartancoder.accommodation.beans.UniversityBean" %>
<%@page import="com.spartancoder.accommodation.beans.CollegeBean" %>
<%@page import="com.spartancoder.accommodation.beans.PlaceBean" %>
<%
    int uId = 0;
    String id = request.getParameter("uId");
    if(id != null) {
        uId = Integer.parseInt(id);
    }

    String cId = request.getParameter("cId");
    String pId = request.getParameter("pId");
    if( cId != null && pId != null ) {
        Place.linkToCollege(Integer.parseInt(pId), Integer.parseInt(cId));
        session.setAttribute("flash", "Legatura a fost setata");
        response.sendRedirect("list-place.jsp?cid=" + cId);
    }
%>

<h2>Ataseaza camin</h2>

<form action="attach-place.jsp" class="span-10" method="post">
    <fieldset>
    <select class="text large" name="uId">
        <% for(UniversityBean ub : University.fetchAll()) { %>
            <option <% if(ub.getUniversityId() == uId) {%>selected="selected"<%}%>
            value="<%=ub.getUniversityId()%>"><%=ub.getName()%></option>
        <% } %>
    </select>
    <button type="submit" class="button positive">Ok</button>
    </fieldset>
</form>

<% if(uId != 0 ) { %>
<form action="attach-place.jsp" class="span-10" method="post">
    <fieldset>
        <input type="hidden" name="uId" value="<%=uId%>" />

        <label class="text large">Facultate:</label><br />
        <select name="cId" class="text large">
        <% for(CollegeBean cb : College.fetchAll(uId)) { %>
            <option value="<%=cb.getCollegeId()%>"><%=cb.getName()%></option>
        <% } %>
        </select><br />

        <label class="text large">Camin:</label><br />
        <select name="pId" class="text large">
        <% for(PlaceBean pb : Place.fetchAll()) { %>
            <option value="<%=pb.getPlaceId()%>"><%=pb.getName()%></option>
        <% } %>
        </select>

        <button type="submit" class="button positive">
            Ataseaza
        </button>
    </fieldset>
</form>
<% } %>

<%@include file="_footer.jspf" %>